import {getTfaList, saveTfa, removeTfa} from '../library/store';

export default {
  namespace: 'index',
  state: {
    tfa: []
  },
  effects: {
    *getTfa(_, { call, put }) {
      const tfaList = yield call(getTfaList);
      yield put({
        type: 'saveTfaList',
        payload: tfaList
      });
    },
    *addTfa({payload}, {call, put}) {
      const tfa = yield call(saveTfa, payload);
      yield put({
        type: 'addTfaToList',
        payload: tfa,
      })
    },
    *removeTfa({payload}, {call, put}) {
      yield call(removeTfa, payload);
      yield put({
        type: 'removeTfaFromList',
        payload,
      });
    }
  },
  reducers: {
    saveTfaList(state, { payload }){
      return {
        tfa: payload
      }
    },
    addTfaToList(state, { payload }) {
      return {
        tfa: [...state.tfa, payload]
      }
    },
    removeTfaFromList(state, { payload }) {
      const {tfa} = state;
      return {
        tfa: tfa.filter(item => item.id !== payload),
      }
    }
  },
};